package com.agan.net.netty_small;

import com.agan.net.utils.ByteUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class HelloWorldClientHandler extends ChannelInboundHandlerAdapter {
    
    private int counter;

    

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        String temp="";
	      byte[] req;
	      ByteBuf message;

	      //滑动窗体 递增
        for(int i=0;i<5;i++){
            temp= ByteUtil.byte512;
            req=temp.getBytes();
            System.out.println(req.length);
            //将上面的所有字符串作为一个消息体发送出去
            message = Unpooled.buffer(req.length);
            message.writeBytes(req);
            ctx.writeAndFlush(message);
        }




        

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String buf = (String)msg;
        System.out.println("==================" + buf );
        System.out.println("++++++++++++++++++"+ (++counter));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}